/*
 * Copyright 2018 JessYan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.jessyan.armscomponent.commonsdk.utils;

import android.app.Activity;
import android.content.Context;

import com.alibaba.android.arouter.launcher.ARouter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * ================================================
 * Created by JessYan on 30/03/2018 17:16
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * ================================================
 */
public class Utils {
    private Utils() {
        throw new IllegalStateException("you can't instantiate me!");
    }

    /**
     * 使用 {@link ARouter} 根据 {@code path} 跳转到对应的页面, 这个方法因为没有使用 {@link Activity}跳转
     * 所以 {@link ARouter} 会自动给 {@link android.content.Intent} 加上 Intent.FLAG_ACTIVITY_NEW_TASK
     * 如果不想自动加上这个 Flag 请使用 {@link ARouter#getInstance()#navigation(Context, String)} 并传入 {@link Activity}
     *
     * @param path {@code path}
     */
    public static void navigation(String path) {
        ARouter.getInstance().build(path).navigation();
    }

    /**
     * 使用 {@link ARouter} 根据 {@code path} 跳转到对应的页面, 如果参数 {@code context} 传入的不是 {@link Activity}
     * {@link ARouter} 就会自动给 {@link android.content.Intent} 加上 Intent.FLAG_ACTIVITY_NEW_TASK
     * 如果不想自动加上这个 Flag 请使用 {@link Activity} 作为 {@code context} 传入
     *
     * @param context
     * @param path
     */
    public static void navigation(Context context, String path) {
        ARouter.getInstance().build(path).navigation(context);
    }


    // 两次点击按钮之间的点击间隔不能少于1000毫秒
    private static final int MIN_CLICK_DELAY_TIME = 1000;
    private static long lastClickTime;

    /**
     * 是否1000ms内点击多次 ：防止重复点击
     *
     * @return false为点击多次
     */
    public static boolean isFastClick() {
        boolean flag = false;
        long currentTime = System.currentTimeMillis();
        if (currentTime - lastClickTime >= MIN_CLICK_DELAY_TIME) {
            flag = true;
        }
        lastClickTime = currentTime;
        return flag;
    }

    /**
     * 获取系统时间戳（10位）
     * @return
     */
    public static long getTimestamp(){
        return System.currentTimeMillis();
    }

    public static long getTimeOfWeekStart(){
        Calendar ca = Calendar.getInstance();
        ca.setFirstDayOfWeek(Calendar.SUNDAY);
        ca.set(Calendar.HOUR_OF_DAY, 0);
        ca.set(Calendar.MINUTE, 0);
        ca.set(Calendar.SECOND, 0);
        ca.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);

        String startTime =  new SimpleDateFormat("yyyy-mm-dd").format(ca.getTime());
        return ca.getTimeInMillis() / 1000;
    }
    public static long getTimeOfWeekEnd(){
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.HOUR_OF_DAY, 0);
        ca.clear(Calendar.MINUTE);
        ca.clear(Calendar.SECOND);
        ca.clear(Calendar.MILLISECOND);
        ca.set(Calendar.DAY_OF_WEEK, ca.getActualMaximum(Calendar.DAY_OF_WEEK));
        String endTime =  new SimpleDateFormat("yyyy-mm-dd").format(ca.getTimeInMillis());
        return ca.getTimeInMillis() / 1000;
    }

    public static String getDateToString(long milSecond) {
        Date date = new Date(milSecond* 1000);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    /**
     *         List<WeekDay> list = getWeekDay();
     *         for (WeekDay weekday : list) {
     *             System.out.println(weekday.toString());
     *         }
     * WeekDay{week='星期日', day='07-23'}
     * WeekDay{week='星期一', day='07-24'}
     * WeekDay{week='星期二', day='07-25'}
     * WeekDay{week='星期三', day='07-26'}
     * WeekDay{week='星期四', day='07-27'}
     * WeekDay{week='星期五', day='07-28'}
     * WeekDay{week='星期六', day='07-29'}
     *
     * @return
     */
    public static List<WeekDay> getWeekDay() {
        Calendar calendar = Calendar.getInstance();
        // 获取本周的第一天
        int firstDayOfWeek = calendar.getFirstDayOfWeek();
        List<WeekDay> list = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            calendar.set(Calendar.DAY_OF_WEEK, firstDayOfWeek + i);
            WeekDay weekDay = new WeekDay();
            // 获取星期的显示名称，例如：周一、星期一、Monday等等
            weekDay.week = calendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.CHINA);
            weekDay.day = new SimpleDateFormat("dd").format(calendar.getTime());
            weekDay.dayLong = new SimpleDateFormat("yyyy-mm-dd").format(calendar.getTime());

            list.add(weekDay);
        }

        return list;
    }

    public static class WeekDay {
        /** 星期的显示名称*/
        public String week;
        /** 对应的日期*/
        public String dayLong;
        public String day;

        @Override
        public String toString() {
            return "WeekDay{" +
                    "week='" + week + '\'' +
                    ", day='" + day + '\'' +
                    '}';
        }
    }
}
